cmake_minimum_required(VERSION 2.6)
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/src)
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/src/thridpart)
LINK_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})

SET(CMAKE_SOURCE_DIR .)
SET(CMAKE_MODULE_PATH ${CMAKE_ROOT}/Modules ${CMAKE_SOURCE_DIR}/cmake/Modules
                ${PROJECT_SOURCE_DIR}/Modules)

configure_file (
	"${PROJECT_SOURCE_DIR}/src/Version.hpp.in"	
	"${PROJECT_SOURCE_DIR}/src/Version.hpp"	
)

SET(AIDP_SRC aidp.cpp
	BootStrap.cpp	
	App.cpp	
	AdServer.cpp
	Http.cpp
	Http/HttpInterface.cpp
	Http/Server.cpp
	Http/Index.cpp
	McProcessor.cpp
	HeadProcessor.cpp
	Aims.cpp
	Aims/Kafka/ConsumerOut.cpp
	Timer.cpp
	App/Message.cpp
	App/Storage.cpp
    App/Metrics.cpp
)
ADD_EXECUTABLE(aidp ${AIDP_SRC})

# adbase
FIND_PACKAGE( libadbase REQUIRED)
MARK_AS_ADVANCED(
LIBADBASE_INCLUDE_DIR
LIBADBASE_LIBRARIES
)
IF (LIBADBASE_INCLUDE_DIR AND LIBADBASE_LIBRARIES)
    MESSAGE(STATUS "Found libadbase libraries")
    INCLUDE_DIRECTORIES(${LIBADBASE_INCLUDE_DIR})
    MESSAGE( ${LIBADBASE_LIBRARIES} )
    TARGET_LINK_LIBRARIES(aidp ${LIBADBASE_LIBRARIES} )
ELSE (LIBADBASE_INCLUDE_DIR AND LIBADBASE_LIBRARIES)
    MESSAGE(FATAL_ERROR "Failed to find libadbase libraries")
ENDIF (LIBADBASE_INCLUDE_DIR AND LIBADBASE_LIBRARIES)

# adbase_kafka
FIND_PACKAGE( libadbase_kafka REQUIRED)
MARK_AS_ADVANCED(
LIBADBASE_KAFKA_INCLUDE_DIR
LIBADBASE_KAFKA_LIBRARIES
)
IF (LIBADBASE_KAFKA_INCLUDE_DIR AND LIBADBASE_KAFKA_LIBRARIES)
    MESSAGE(STATUS "Found libadbase_kafka libraries")
    INCLUDE_DIRECTORIES(${LIBADBASE_KAFKA_INCLUDE_DIR})
    MESSAGE( ${LIBADBASE_KAFKA_LIBRARIES} )
    TARGET_LINK_LIBRARIES(aidp ${LIBADBASE_KAFKA_LIBRARIES} )
ELSE (LIBADBASE_KAFKA_INCLUDE_DIR AND LIBADBASE_KAFKA_LIBRARIES)
    MESSAGE(FATAL_ERROR "Failed to find libadbase_kafka libraries")
ENDIF (LIBADBASE_KAFKA_INCLUDE_DIR AND LIBADBASE_KAFKA_LIBRARIES)

# rdkafka++
FIND_PACKAGE( librdkafka REQUIRED)
MARK_AS_ADVANCED(
        LIBRDKAFKA_INCLUDE_DIR
        LIBRDKAFKA_LIBRARIES
)
IF (LIBRDKAFKA_INCLUDE_DIR AND LIBRDKAFKA_LIBRARIES)
        MESSAGE(STATUS "Found librdkafka libraries")
        INCLUDE_DIRECTORIES(${LIBRDKAFKA_INCLUDE_DIR})
        MESSAGE( ${LIBRDKAFKA_LIBRARIES} )
        TARGET_LINK_LIBRARIES(aidp ${LIBRDKAFKA_CPP_LIBRARIES} )
        TARGET_LINK_LIBRARIES(aidp ${LIBRDKAFKA_LIBRARIES} )
ELSE (LIBRDKAFKA_INCLUDE_DIR AND LIBRDKAFKA_LIBRARIES)
        MESSAGE(FATAL_ERROR "Failed to find librdkafka libraries")
ENDIF (LIBRDKAFKA_INCLUDE_DIR AND LIBRDKAFKA_LIBRARIES)

# adbase_lua
FIND_PACKAGE( libadbase_lua REQUIRED)
MARK_AS_ADVANCED(
    LIBADBASE_LUA_INCLUDE_DIR
    LIBADBASE_LUA_LIBRARIES
)
IF (LIBADBASE_LUA_INCLUDE_DIR AND LIBADBASE_LUA_LIBRARIES)
    MESSAGE(STATUS "Found libadbase_kafka libraries")
    INCLUDE_DIRECTORIES(${LIBADBASE_LUA_INCLUDE_DIR})
    MESSAGE( ${LIBADBASE_LUA_LIBRARIES} )
    TARGET_LINK_LIBRARIES(aidp ${LIBADBASE_LUA_LIBRARIES} )
ELSE (LIBADBASE_LUA_INCLUDE_DIR AND LIBADBASE_LUA_LIBRARIES)
    MESSAGE(FATAL_ERROR "Failed to find libadbase_lua libraries")
ENDIF (LIBADBASE_LUA_INCLUDE_DIR AND LIBADBASE_LUA_LIBRARIES)

# liblua
FIND_PACKAGE( liblua REQUIRED)
MARK_AS_ADVANCED(
    LIBLUA_INCLUDE_DIR
    LIBLUA_LIBRARIES
)
IF (LIBLUA_INCLUDE_DIR AND LIBLUA_LIBRARIES)
    MESSAGE(STATUS "Found liblua libraries")
    INCLUDE_DIRECTORIES(${LIBLUA_INCLUDE_DIR})
    MESSAGE( ${LIBLUA_LIBRARIES} )
    TARGET_LINK_LIBRARIES(aidp ${LIBLUA_LIBRARIES} )
ELSE (LIBLUA_INCLUDE_DIR AND LIBLUA_LIBRARIES)
    MESSAGE(FATAL_ERROR "Failed to find liblua libraries")
ENDIF (LIBLUA_INCLUDE_DIR AND LIBLUA_LIBRARIES)

# pthread
#FIND_PACKAGE( libpthread REQUIRED)
#MARK_AS_ADVANCED(
#LIBPTHREAD_INCLUDE_DIR
#LIBPTHREAD_LIBRARIES
#)
#IF (LIBPTHREAD_INCLUDE_DIR AND LIBPTHREAD_LIBRARIES)
#    MESSAGE(STATUS "Found libpthread libraries")
#    INCLUDE_DIRECTORIES(${LIBPTHREAD_INCLUDE_DIR})
#    MESSAGE( ${LIBPTHREAD_LIBRARIES} )
#    TARGET_LINK_LIBRARIES(aidp ${LIBPTHREAD_LIBRARIES} )
#ELSE (LIBPTHREAD_INCLUDE_DIR AND LIBPTHREAD_LIBRARIES)
#    MESSAGE(FATAL_ERROR "Failed to find libpthread libraries")
#ENDIF (LIBPTHREAD_INCLUDE_DIR AND LIBPTHREAD_LIBRARIES)

TARGET_LINK_LIBRARIES(aidp libevent.a pthread rt dl libz.a)

INSTALL(TARGETS aidp RUNTIME DESTINATION bin)
